#include <stdio.h>
#include <glm/glm.hpp>
#include "AnimatedGate.h"
#include "Transform.h"
#include "TexturedSquare.h"
#include "variables.h"

AnimatedGate::AnimatedGate(const char* texture_filename)
{
	rotate_degrees = 0.0f;
	texID = TexturedSquare::loadTexture(texture_filename);
}


AnimatedGate::~AnimatedGate(void)
{
}

void AnimatedGate::toggleState(int &doorstate){
	if( doorstate == OPEN){
		doorstate = CLOSING;
		printf("Door closing\n");
	}else if (doorstate == CLOSED){
		doorstate = OPENING;
		printf("Door opening\n");
	}else{
		//animation is in progress; do nothing
	}
}

//1 = right, -1 = left
static void drawDoor(float leftright){
	  const float top_z = .3;
	  const float bot_z = -.5;
	  const float depth = .05;
	  const float width = leftright*-.5;
	  glBegin(GL_QUADS); //front
		glTexCoord2f(0 ,0);
		glVertex3f(0,0, bot_z);
		glTexCoord2f(0 ,1);
		glVertex3f(0,0,top_z);
		glTexCoord2f(1 ,1);
		glVertex3f(width,0, top_z);
		glTexCoord2f(1 ,0);
		glVertex3f(width,0, bot_z);
	  glEnd();

	  glBegin(GL_QUADS); //back
		glVertex3f(0,depth, bot_z);
		glVertex3f(0,depth,top_z);
		glVertex3f(width,depth, top_z);
		glVertex3f(width,depth, bot_z);
	  glEnd();

	  glBegin(GL_QUADS); //top
		glVertex3f(0,0, top_z);
		glVertex3f(0,depth,top_z);
		glVertex3f(width,depth, top_z);
		glVertex3f(width,0, top_z);
	  glEnd();

	  glBegin(GL_QUADS); //left
		glVertex3f(width,0, top_z);
		glVertex3f(width,depth,top_z);
		glVertex3f(width,depth, bot_z);
		glVertex3f(width,0, bot_z);
	  glEnd();
}

void AnimatedGate::draw(glm::mat4 global_transform){

		  if( doorstate == CLOSED){
			rotate_degrees = 0;
		  }else if (doorstate == OPEN){
			rotate_degrees = 90;
		  }else if (doorstate == OPENING){
			rotate_degrees += .5;

			if( rotate_degrees >= 90.0){
				doorstate = OPEN;
				printf("Door opened!\n");
			}
		  }else if (doorstate == CLOSING){
			rotate_degrees -= .5;		

			if( rotate_degrees <= 0.0){
				doorstate = CLOSED;
				printf("Door closed!\n");
			}
		  }
	      //rotate_degrees = .36*((float)animate);

		  glUniform4fv(ambientcol ,1, this->ambient);
		  glUniform4fv(diffusecol ,1, this->diffuse );
		  glUniform4fv(specularcol ,1, this->specular );
		  glUniform4fv( emissioncol ,1, this->emission);
		  glUniform1f(shininesscol, this->shininess);

		  glUniform1i( textured_object, 1);
		  glUniform1i( texture_location, 0);
		  glBindTexture( GL_TEXTURE_2D, this->texID);

		  glm::mat4 transf = ((mat4)Transform::rotate(rotate_degrees, vec3(0,0,1)))*Transform::translate(.5,0,0)*(this->transform);
		  glm::mat4 matleft = glm::transpose( (transf)*(global_transform));
		  glLoadMatrixf(&matleft[0][0]);
		  drawDoor(1.0);//right

		  transf =  ((mat4)Transform::rotate(-rotate_degrees, vec3(0,0,1)))*Transform::translate(-.5,0,0)*(this->transform); 
		  glm::mat4 matright = glm::transpose( (transf)*(global_transform));
		  glLoadMatrixf(&matright[0][0]);
		  drawDoor(-1.0);//left

		  glUniform1i( textured_object, 0);
}